﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.HCLoad.Annotate;
using YArchitech.HCLoad.Controls;
using YArchitech.HCLoad.DataBinding;
using YArchitech.HCLoad.Parameters;
using YArchitech.HCLoad.Properties;
using YArchitech.HCLoad.Utils;
using YArchitech.HCLoad.XmlData;
using YArchitech.LIB;

namespace YArchitech.HCLoad.Import
{
	public partial class ImportForm : System.Windows.Forms.Form
	{
		public ImportForm(UIApplication UIApp, bool bAnnotate)
		{
			this.m_UIApplication = UIApp;
			this.InitializeComponent();
			this.m_bAnnotate = bAnnotate;
			this.m_SpaceAttributeFile = new SpaceAttributeFile();
		}

		private void ImportForm_Layout(object sender, LayoutEventArgs e)
		{
			System.Drawing.Rectangle displayRectangle = this.label_SelectFile.DisplayRectangle;
			System.Drawing.Rectangle displayRectangle2 = this.groupBox_annotate_options.DisplayRectangle;
			if (this.m_bAnnotate)
			{
				int num = displayRectangle2.Top - displayRectangle.Top - 8;
				this.groupBox_annotate_options.Top -= num;
				this.groupBox_annotate_options.Height += num;
				this.treeView_Annotate_Options.Height += num;
			}
		}

		private void ImportForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			if (this.m_bAnnotate)
			{
				this.label_SelectFile.Hide();
				this.textBox_FileName.Hide();
				this.button_SelectFile.Hide();
				this.Text = YArchitech.HCLoad.Properties.Resources.RIBBON_BUTTON_ANNOTATE_NAME;
				this.button_UpdateSpace.Text = YArchitech.HCLoad.Properties.Resources.RIBBON_BUTTON_ANNOTATE_NAME;
			}
			HcRegistry.GetRegValue("SourceFileName", ref this.m_sSourceFileName);
			this.textBox_FileName.Text = this.m_sSourceFileName;
			if (this.m_SpaceAttributeFile == null)
			{
				this.m_SpaceAttributeFile = new SpaceAttributeFile();
			}
			this.m_SpaceAttributeFile.Load(HcUtils.SpaceParametersFile);
			this.InitTree();
		}

		private void button_Cancel_Click(object sender, EventArgs e)
		{
			base.Close();
			base.DialogResult = DialogResult.Cancel;
		}

		private void button_SelectFile_Click(object sender, EventArgs e)
		{
			if (this.m_bAnnotate)
			{
				return;
			}
			string text = this.textBox_FileName.Text;
			OpenFileDialog openFileDialog = new OpenFileDialog();
			if (text != null && text.Length > 0)
			{
				openFileDialog.InitialDirectory = Path.GetDirectoryName(text);
				openFileDialog.FileName = Path.GetFileNameWithoutExtension(text);
			}
			openFileDialog.Filter = HcUtils.ConstructFileFilter("hclx");
			openFileDialog.FilterIndex = 1;
			openFileDialog.RestoreDirectory = true;
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				this.m_sSourceFileName = openFileDialog.FileName;
				this.textBox_FileName.Clear();
				this.textBox_FileName.Paste(this.m_sSourceFileName);
			}
		}

		private void InitTree()
		{
			HcTriStateTreeView hcTriStateTreeView = this.treeView_Annotate_Options;
			hcTriStateTreeView.Nodes.Clear();
			foreach (SpaceAttributeGroup spaceAttributeGroup in this.m_SpaceAttributeFile.Groups)
			{
				if (spaceAttributeGroup.PropId >= 0 && !("4" == spaceAttributeGroup.DataCategory))
				{
					TreeNode treeNode = hcTriStateTreeView.Nodes.Add(spaceAttributeGroup.Name);
					treeNode.Tag = spaceAttributeGroup;
					foreach (SpaceAttribute spaceAttribute in spaceAttributeGroup.Attributes)
					{
						if (spaceAttribute.PropId >= 0)
						{
							TreeNode treeNode2 = treeNode.Nodes.Add(spaceAttribute.Name);
							treeNode2.Tag = spaceAttribute;
							treeNode2.Checked = spaceAttribute.Cheked;
						}
					}
					if ("1" == spaceAttributeGroup.DataCategory)
					{
						treeNode.Expand();
					}
				}
			}
			hcTriStateTreeView.Refresh();
			this.UpdateTreeState();
		}

		private void UpdateTreeState()
		{
			foreach (object obj in this.treeView_Annotate_Options.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				treeNode.StateImageIndex = this.GetState(treeNode);
			}
		}

		private int GetState(TreeNode treeNode)
		{
			int num = 0;
			IEnumerator enumerator = treeNode.Nodes.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((TreeNode)enumerator.Current).Checked)
					{
						num++;
					}
				}
			}
			if (num == 0)
			{
				return 0;
			}
			if (num == treeNode.Nodes.Count)
			{
				return 1;
			}
			return 2;
		}

		private bool BindingYArchitechParameters()
		{
			return SpaceSharedParameters.Binding(HcUtils.SharedParametersFile, this.m_UIApplication);
		}

		private bool CheckSourceFile()
		{
			if (this.m_sSourceFileName.Length == 0)
			{
				MessageBox.Show(YArchitech.HCLoad.Properties.Resources.Error_SourceFileName_Null);
				return false;
			}
			if (!File.Exists(this.m_sSourceFileName))
			{
				MessageBox.Show(YArchitech.HCLoad.Properties.Resources.Error_SourceFileName_NotExist);
				return false;
			}
			return true;
		}

		private void SaveAnnotateOption()
		{
			foreach (object obj in this.treeView_Annotate_Options.Nodes)
			{
				foreach (object obj2 in ((TreeNode)obj).Nodes)
				{
					TreeNode treeNode = (TreeNode)obj2;
					(treeNode.Tag as SpaceAttribute).Cheked = treeNode.Checked;
				}
			}
			this.m_SpaceAttributeFile.Save();
		}

		private void button_UpdateSpace_Click(object sender, EventArgs e)
		{
			this.SaveAnnotateOption();
			if (!this.m_SpaceAttributeFile.IsAnyItemChecked() && DialogResult.Yes != MessageBox.Show("没有选择标注项，将不会标注计算结果。原有标注内容将被删除！ \r\n要继续么？", "盈建科提示", MessageBoxButtons.YesNo))
			{
				return;
			}
			if (!this.m_bAnnotate)
			{
				this.m_sSourceFileName = this.textBox_FileName.Text;
				if (!this.CheckSourceFile())
				{
					return;
				}
				HcRegistry.SetRegValue("SourceFileName", this.m_sSourceFileName);
				DataBase hcdb = new DataBase();
				if (!HcUtils.LoadHclxFile(this.m_sSourceFileName, ref hcdb))
				{
					return;
				}
				this.BindingYArchitechParameters();
				if (!DatabaseBinding.Binding(this.m_UIApplication, hcdb, this.m_SpaceAttributeFile))
				{
					base.DialogResult = DialogResult.Cancel;
					return;
				}
				if (DatabaseBinding.DataChanged)
				{
					if (DialogResult.Yes != MessageBox.Show(YArchitech.HCLoad.Properties.Resources.Error_DataChanged, YArchitech.HCLoad.Properties.Resources.MSGBOX_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
					{
						base.DialogResult = DialogResult.Cancel;
						base.Close();
						return;
					}
					string text = Path.GetDirectoryName(this.m_sSourceFileName) + "\\" + Path.GetFileNameWithoutExtension(this.m_sSourceFileName);
					text += "_Changes.xml";
					if (DatabaseBinding.SaveConstructionChanges(text))
					{
						MessageBox.Show(YArchitech.HCLoad.Properties.Resources.DataChange_Saved + text, YArchitech.HCLoad.Properties.Resources.MSGBOX_TITLE);
					}
				}
			}
			new SpaceAnnotate(this.m_UIApplication, this.m_SpaceAttributeFile).AnnotateAllSpace();
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private const string sXmlFileExternsion = "hcXml";

		private const string sHclxFileExternsion = "hclx";

		private string sYArchitechSpaceParametersFile = HcUtils.SpaceParametersFile;

		private bool m_bAnnotate;

		private bool m_bEn = HcUtils.IsEnglish;

		private string m_sSourceFileName;

		private UIApplication m_UIApplication;

		private SpaceAttributeFile m_SpaceAttributeFile;
	}
}
